package com.dfcd.commonlib.retrofit;


import com.dfcd.commonlib.utils.MLog;
import okhttp3.Interceptor;
import okhttp3.Request;

import java.io.IOException;

/**
 * 自定义的一个日志过滤器
 *
 * @author zengyi
 * create at 2017/1/10 9:40
 */
public class LogInterceptor implements Interceptor {
	public String TAG;


	public LogInterceptor(String tag) {
		TAG = tag;
	}

	@Override
	public okhttp3.Response intercept(Chain chain) throws IOException {
		Request request = chain.request();
		MLog.e(TAG, "request's url " + request.url());//打印请求URL
		long t1 = System.currentTimeMillis();
		okhttp3.Response response = chain.proceed(chain.request());
		long t2 = System.currentTimeMillis();
		MLog.e(TAG, "request's time  " + (t2 - t1) + "");//打印请求所需要的时间
		okhttp3.MediaType mediaType = response.body().contentType();
		String content = response.body().string();
		MLog.e(TAG, "response body:  " + content);//打印请求得到的结果
		return response.newBuilder()
				.body(okhttp3.ResponseBody.create(mediaType, content))
				.build();
	}
}